<?php
class Library extends CI_Controller
{
	private $data = array();

	function __construct(){
		parent::__construct();
		// autoload helper and library
		$this->layout->setLayout('admin');
		$this->load->model('library_model');
		$this->load->model('project_model');
		$this->data['error'] = '';
		$auth = $this->session->userdata('auth');
		if(!$auth)
		{
			redirect('/auth/login');
		}
	}
	function index(){
		$projectid = $this->uri->segment(3);
		if($projectid){
			$config['base_url'] =base_url()."index.php/library/index/".$projectid;
			$this->db->where('idproject',$projectid);
			$this->db->from('libary');
			$config['total_rows'] = $this->db->count_all_results();
			$config['per_page'] = '10';
			$config['uri_segment'] = 4;
			$this->pagination->initialize($config);
			//-------------
			$projectid = $this->uri->segment(3);
			$this->data['project'] = $this->project_model->getProject($projectid);
			$offset = $this->uri->segment(4);
			$library = $this->library_model->getListLibrary($offset,$config['per_page'],$projectid);
			$this->data['library'] = $library;
			$this->layout->view('listlibrary',$this->data);
		}else{
			redirect('/404/');
		}
	}
	function addLibrary(){
		$projectid = $this->uri->segment(3);
		if($projectid){
			$this->data['project'] = $this->project_model->getProject($projectid);
			$this->data['library']->id = $this->library_model->getLastLibraryId() + 1;
			$this->data['library']->image = '';
			$this->data['action'] = "submitAddLibrary";
			$this->layout->view('addLibrary',$this->data);
		}else{
			redirect('/404/');
		}
	}
	function submitAddLibrary(){
		$projectId = $this->uri->segment(3);
		if($projectId){
			$config['upload_path'] = "upload/library/";
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '2048';
			$config['encrypt_name']	= TRUE;
			$this->load->library('upload', $config);
			if(!$this->upload->do_upload('file')){
				$error = $this->upload->display_errors();
				$this->session->set_flashdata('error', $error);
				redirect('library/addLibrary/'.$projectId,'location', 301);
			}
			else {
				$file = $this->upload->data();
				$dt['idproject'] = $projectId;
				$dt['image'] = $file['file_name'];
				$this->library_model->addLibrary($dt);
				redirect('library/index/'.$projectId, 'location', 301);
			}
		}else{
			redirect('/404/');
		}
	}
	function delete(){
		$libraryId = $this->input->post('hidID');
		$arrLibraryId = $this->input->post('chkid');
		$projectID = $this->input->post('projectID');
		var_dump($projectID);
		if($libraryId){
			$this->library_model->delete($libraryId);
		}
		else{
			foreach ($arrLibraryId as $id):
			$this->library_model->delete($id);
			endforeach;
		}
		redirect('library/index/'.$projectID);
	}
	function update(){
		$projectid = $this->uri->segment(3);
		$libraryid = $this->uri->segment(4);
		if($projectid && $libraryid){
			$this->data['project'] = $this->project_model->getProject($projectid);
			$this->data['library'] = $this->library_model->getLibrary($libraryid);
			$this->data['action'] = "submitUpdateLibrary";
			$this->layout->view('addLibrary',$this->data);
		}else{
			redirect('/404/');
		}
	}
	function submitUpdateLibrary(){
			$projectId = $this->uri->segment(3);
			$libraryId = $this->uri->segment(4);
		if($projectId && $libraryId){
			$this->data['project'] = $this->project_model->getProject($projectId);
			$this->data['library'] = $this->library_model->getLibrary($libraryId);
			$this->data['action'] = "submitUpdateLibrary";

			$config['upload_path'] = "upload/library/";
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '2048';
			$config['encrypt_name']	= TRUE;
			$this->load->library('upload', $config);
			if(!$this->upload->do_upload('file')){
				$error = $this->upload->display_errors();
				$this->session->set_flashdata('error', $error);
				redirect('library/update/'.$projectId.'/'.$libraryId,'location', 301);
			}
			else {
				$file = $this->upload->data();
				$dt['id'] = $libraryId;
				$dt['image'] = $file['file_name'];
				$this->library_model->updateLibrary($dt);
				redirect('library/index/'.$projectId, 'location', 301);
			}
		}else{
			redirect('/404/');
		}
	}
}